<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Student extends Model {
    //指定表名
    protected $table = 'student';

    //指定主键  find方法调用
    protected $primaryKey = 'id';

    //不自定维护时间  数据类型必须为public  时间字段默认字段名create_at  update_at
    public $timestamps = false;

    //定义可批量增加的字段
    protected $fillable = ['name', 'age', 'sex', 'create_time'];

    //定义不可批量增加的字段
    protected $guarded = ['name', 'age', 'sex', 'create_time'];


    /**
     * 描述 : 若数据表时间字段为时间戳
     * 作者 : gxh
     * @return int|string
     */
    public function getDateFormat (){
        return time();
    }

    /**
     * 描述 : 不格式化时间戳
     * 参数 : $val
     * 返回 : 返回时间戳
     * 作者 : gxh
     * @return \Illuminate\Support\Carbon|mixed
     */
    public function asDateTime ($val){
        return $val;
    }

    public static function index($id) {
        if ($id) {
            $students = DB::select('select * from student');
        } else {
            //$students = DB::insert("insert into student(`name`,`sex`,`age`,`create_time`) values('gxh',1,120,NOW())");
            $students = DB::insert("insert into student(`name`,`sex`,`age`,`create_time`) values(?,?,?,?)",
                [
                    'dara', 1, 18, NOW()
                ]);
            $students = DB::update("update student set `name`=?,`update_time`=? where id=?",
                [
                    'Gongxiaohua', NOW(), 2
                ]);

            $students = DB::delete("delete from student where id=?", [2]);

        }
        return $students;
    }

    public static function add() {
        //直接插入单条
        $res = DB::table('student')->insert([
            'name'        => 'zhuwenxing',
            'age'         => '24',
            'sex'         => 1,
            'create_time' => NOW(),
        ]);

        //直接插入多条
        $res = DB::table('student')->insert([
            [
                'name'        => 'wen',
                'age'         => '24',
                'sex'         => 1,
                'create_time' => NOW(),
            ],
            [
                'name'        => 'zhu',
                'age'         => '188',
                'sex'         => 1,
                'create_time' => NOW(),
            ]
        ]);

        //获取自增id
        $res = DB::table('student')->insertGetId([
            'name'        => 'xing',
            'age'         => '24',
            'sex'         => 0,
            'create_time' => NOW(),
        ]);
        return $res;
    }

    public static function update1() {
        //带条件更新
        $res = DB::table('student')
            //->where(['id' => 10])
            ->where('id', 10)
            ->update([
                'age'         => 150,
                'update_time' => NOW(),
            ]);

        //自增  返回受影响行数
        $res = DB::table('student')
            ->increment('age');
        //自减  返回受影响行数
        $res = DB::table('student')
            ->where([
                    'id' => 7
                ])
            ->decrement('age', 16, ['update_time' => NOW()]);
        return $res;
    }

    public static function del() {
        //返回删除的行数
        $res = DB::table('student')
            ->where([
                'id' => 7
            ])
            ->delete();

        $res = DB::table('student')
            ->delete(['id' => 10]);

        $res = DB::table('student')
            ->where('id', '>=', 3)
            ->delete();
        //不返回，打印为null
        $res = DB::table('student')
            ->truncate();
        return $res;
    }

    public static function query() {
        //get
        $res = DB::table('student')
            ->orderBy('id', 'desc')
            ->get(['id','age']);

        //first  获取一条数据
        $res = DB::table('student')
            ->orderBy('id', 'desc')
            ->first(['id','age']);

        //where
        $res = DB::table('student')
            ->where('id', 1)
            ->get();

        //whereRaw
        $res = DB::table('student')
            ->whereRaw('id >= ? and age = ?', [3, 24])
            ->get();

        //pluck lists  1.返回结果集中的指定字段  array_values
        $res = DB::table('student')
            ->orderBy('id', 'asc')
            ->pluck('name', 'id');

        //chunk    array_chunk 分段获取（必须加上orderby以起使用）
        $res = DB::table('student')
            ->orderBy('id', 'asc')
            ->chunk(2,function ($students){
                print_r($students);
            });

        //select  返回指定字段
        $res = DB::table('student')
            ->whereRaw('id >= ? and age = ?', [3, 24])
            ->select('id','name','create_time')
            ->get();
        return $res;
    }

    /**
     * 描述 : 聚合函数
     * 作者 : gxh
     */
    public static function groupFunc() {
        $res = DB::table('student')
            ->count();
        $res = DB::table('student')
            ->max('age');
        $res = DB::table('student')
            ->min('age');
        $res = DB::table('student')
            ->avg('age');
        $res = DB::table('student')
            ->sum('age');
        return $res;
    }

}
